import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
 
 
class MergeCell(QWidget):
    def __init__(self):
        super(MergeCell, self).__init__()
        self.initUI()
 
    def initUI(self):
        self.setWindowTitle('合并单元格')
        self.resize(430, 230)
 
        layout = QHBoxLayout()
        #表格对象
        tableWidget=QTableWidget()
        #四行三列
        tableWidget.setRowCount(4)
        tableWidget.setColumnCount(3)
        #设置表格字段
        tableWidget.setHorizontalHeaderLabels(['姓名','性别','体重(kg)'])
 
        newItem=QTableWidgetItem('小明')
        tableWidget.setItem(0,0,newItem)
        #setSpan(row, col, 要合并的行数，要合并的列数)
        tableWidget.setSpan(0,0,3,1)
 
        newItem = QTableWidgetItem('男')
        tableWidget.setItem(0, 1, newItem)
        tableWidget.setSpan(0, 1, 2, 1)
 
        newItem = QTableWidgetItem('190')
        tableWidget.setItem(0, 2, newItem)
 
        newItem=QTableWidgetItem('test')
        tableWidget.setItem(2,1,newItem)
        tableWidget.setSpan(2,1,1,2)
 
        layout.addWidget(tableWidget)
        self.setLayout(layout)
 
 
if __name__=='__main__':
    app=QApplication(sys.argv)
    main=MergeCell()
    main.show()
    sys.exit(app.exec_())
 
